package dao;

import bean.Config;
import util.JdbcUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * The type Config dao.
 *
 * @author dqy
 */
public class ConfigDao {

    /**
     * Gets config.
     *
     * @param value the value
     * @return the config
     * @throws Exception the exception
     */
    public Config getConfig(String value) throws Exception {
        String sql = "select * from config where name=?";
        List<Object> para = new ArrayList<>();
        para.add(value);
        List<Object> list = JdbcUtils.queryForArrObject(sql, para, Config.class);
        return list.isEmpty() ? null : (Config) list.get(0);
    }

    /**
     * Update config boolean.
     *
     * @param config the config
     * @return the boolean
     * @throws Exception the exception
     */
    public boolean updateConfig(Config config) throws Exception {
        String sql = "update config set value=? where name=?";
        List<Object> para = new ArrayList<Object>();
        para.add(config.getValue());
        para.add(config.getName());
        return 1 == JdbcUtils.update(sql, para);
    }

    /**
     * Gets all config.
     *
     * @return the all config
     */
    public List<Object> getAllConfig() {
        String sql = "select * from config where name='price' or name='activityReword' or name='adviceReword'";
        List<Object> list = JdbcUtils.queryForArrObject(sql, null, Config.class);
        return list.isEmpty() ? null : list;
    }
}

